package com.x.ant.util.pdf;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

public class CreatePDF {
    public static void main(String[] args) {
        CreatePDF p001 = new CreatePDF();

        String filename = "P001.pdf";
        p001.createPDF(filename);
    }

    public void createPDF(String filename) {
        // step 1
        Document document = new Document(PageSize.A4);
        // step 2
        try {
            BaseFont bf=BaseFont.createFont( "STSong-Light",   "UniGB-UCS2-H",   BaseFont.NOT_EMBEDDED);
            Font font=new Font(bf,22,Font.NORMAL);
            PdfWriter.getInstance(document, new FileOutputStream(filename));

            document.addTitle("黑蚁优服");
            document.addAuthor("x");
            document.addSubject("This is the subject of the PDF file.");
            document.addKeywords("This is the keyword of the PDF file.");

            // step 3
            document.open();
            // step 4
            PdfGState gs = new PdfGState();
            gs.setFillOpacity(0.2f);// 设置透明度为0.2

            Image image = Image.getInstance("bg.png");
            document.add(image);
//            document.add(gs);
            boolean add = document.add(new Paragraph("你好你好你好你好你好你好你好你好你好你好你好!", font));
            document.add(new Paragraph(" ", font));

            PdfPTable table = createTable1();
            document.add(table);


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // step 5
            document.close();
        }
    }

    public static PdfPTable createTable1() throws DocumentException {
        PdfPTable table = new PdfPTable(3);
        table.setWidthPercentage(288 / 5.23f);
        table.setWidths(new int[] { 2, 1, 1 });

        PdfPCell cell;
        cell = new PdfPCell(new Phrase("Table 1"));
        cell.setColspan(3);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
        cell.setRowspan(2);
        table.addCell(cell);
        table.addCell("row 1; cell 1");
        table.addCell("row 1; cell 2");
        table.addCell("row 2; cell 1");
        table.addCell("row 2; cell 2");
        return table;
    }

}
